package org.jivesoftware.smackx.packet;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.RemoteRosterEntry;

public class RosterExchange
  implements PacketExtension
{
  private List remoteRosterEntries = new ArrayList();

  public RosterExchange(Roster paramRoster)
  {
    Iterator localIterator = paramRoster.getEntries().iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
        return;
      addRosterEntry((RosterEntry)localIterator.next());
    }
  }

  public void addRosterEntry(RosterEntry paramRosterEntry)
  {
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator = paramRosterEntry.getGroups().iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
      {
        String[] arrayOfString = (String[])localArrayList.toArray(new String[localArrayList.size()]);
        addRosterEntry(new RemoteRosterEntry(paramRosterEntry.getUser(), paramRosterEntry.getName(), arrayOfString));
        return;
      }
      localArrayList.add(((RosterGroup)localIterator.next()).getName());
    }
  }

  public void addRosterEntry(RemoteRosterEntry paramRemoteRosterEntry)
  {
    synchronized (this.remoteRosterEntries)
    {
      this.remoteRosterEntries.add(paramRemoteRosterEntry);
      return;
    }
  }

  public String getElementName()
  {
    return "x";
  }

  public int getEntryCount()
  {
    return this.remoteRosterEntries.size();
  }

  public String getNamespace()
  {
    return "jabber:x:roster";
  }

  public Iterator getRosterEntries()
  {
    synchronized (this.remoteRosterEntries)
    {
      Iterator localIterator = Collections.unmodifiableList(new ArrayList(this.remoteRosterEntries)).iterator();
      return localIterator;
    }
  }

  public String toXML()
  {
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append("\">");
    Iterator localIterator = getRosterEntries();
    while (true)
    {
      if (!(localIterator.hasNext()))
      {
        localStringBuilder.append("</").append(getElementName()).append(">");
        return localStringBuilder.toString();
      }
      localStringBuilder.append(((RemoteRosterEntry)localIterator.next()).toXML());
    }
  }
}